using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GMGlobal : MonoBehaviour
{
    // 创建该类单例
    public static GMGlobal Instance;
    [HideInInspector]
    /// <summary>
    /// 鸟生命值
    /// </summary>
    public int birdLive = 3;
    [HideInInspector]
    /// <summary>
    /// 鸟状态
    /// </summary>
    public int birdStatus = 1;    // 鸟状态：0 死亡；1 正常存活；2 囚禁
    [HideInInspector]
    public float velocity=1f;
    [HideInInspector]
    /// <summary>
    /// 飞行距离
    /// </summary>
    public float flight = 0;


    [Header("配置项")]
    public GameObject player;


    public List<GameObject> birdPrefabs;

    private int birdID;
    private GameObject birdObj;

    private GameObject lives;
    // Start is called before the first frame update
    private void Awake()
    {
        //绑定一下该单例
        Instance = this;
        lives = GameObject.Find("Lives");
    }
    void Start()
    {
        birdID = PlayerPrefs.GetInt("birdID");
        birdObj = Instantiate(birdPrefabs[birdID]);
        birdObj.transform.parent = player.transform;
        birdObj.transform.localPosition = new Vector2(0, 0);

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void UpdateLive()
    {
        for (int i = 0; i < lives.transform.childCount; i++)
        {
            Transform child= lives.transform.GetChild(i);
            if(i<Instance.birdLive)
            {
                child.gameObject.SetActive(true);
            }
            else
            {
                child.gameObject.SetActive(false);
            }
        }
    }

    /// <summary>
    /// 清除父物体下面的所有子物体
    /// </summary>
    /// <param name="parent"></param>
    public void ClearChilds(Transform parent)
    {
        if (parent.childCount > 0)
        {
            for (int i = 0; i < parent.childCount; i++)
            {
                Destroy(parent.GetChild(i).gameObject);
            }
        }
    }
}
